﻿using CLROBS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DesertKharonIrcPlugin
{
    public class DesertKharonIrcPlugin : AbstractPlugin
    {
        private string _referencedDllFolderPath;
        public DesertKharonIrcPlugin()
        {
            // Yeah, it's depricated. I don't care. Nobody does. It's depricated since .NET 2.0 (8 years)
            // Microsoft can't imagine why someone would want to add a subfolder to search for dlls *facepalm* ...
            // http://blogs.msdn.com/b/junfeng/archive/2005/02/20/377177.aspx
            if (AppDomain.CurrentDomain.BaseDirectory.DirectoryPathEndsWith("plugins\\CLRHostPlugin"))
                AppDomain.CurrentDomain.AppendPrivatePath("DesertKharonIrc");
            else
                AppDomain.CurrentDomain.AppendPrivatePath("plugins\\CLRHostPlugin\\DesertKharonIrc");

            //// Instead do this ...
            //Assembly desertKharonIrcAssembly = Assembly.GetAssembly(typeof(DesertKharonIrcPlugin));
            //if (desertKharonIrcAssembly != null)
            //{
            //    _referencedDllFolderPath = Path.Combine(new Uri(Path.GetDirectoryName(desertKharonIrcAssembly.CodeBase)).LocalPath, "DesertKharonIrc");
            //    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            //}

            Name = "Desert Kharon Irc";
            Description = "Connects to an irc server and displays messages of one selected channel.";

            AssureEmoteFolderExists();
        }

        //private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        //{
        //    if (!String.IsNullOrWhiteSpace(_referencedDllFolderPath))
        //    {
        //        if (args.Name.StartsWith("Meebey.SmartIrc4net"))
        //        {
        //            string meebeySmartIrc4NetPath = Path.Combine(_referencedDllFolderPath, "Meebey.SmartIrc4net.dll");
        //            if (File.Exists(meebeySmartIrc4NetPath))
        //                return Assembly.LoadFrom(meebeySmartIrc4NetPath);
        //        }
        //    }
        //
        //    return null;
        //}

        public override bool LoadPlugin()
        {
            API.Instance.AddImageSourceFactory(new DesertKharonIrcImageSourceFactory());
            return true;
        }

        public static string GetEmoteFolder()
        {
            return CreateEmoteFolder();
        }

        public static void AssureEmoteFolderExists()
        {
            CreateEmoteFolder();
        }

        private static string CreateEmoteFolder()
        {
            string applicationDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            string desertKharonFolder = Path.Combine(applicationDataFolder, "Desert Kharon Irc");
            if (!Directory.Exists(desertKharonFolder))
                Directory.CreateDirectory(desertKharonFolder);

            string emoteFolder = Path.Combine(desertKharonFolder, "Emotes");
            if (!Directory.Exists(emoteFolder))
                Directory.CreateDirectory(emoteFolder);

            return emoteFolder;
        }
    }
}
